package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.PointGiveLog;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

@ControllerBind(controllerKey = "/clmp/pointGiveLog")
public class PointGiveLogController extends WebApiController {
    @Override
    public void index() {
        PointGiveLog pointGiveLog = getModel(PointGiveLog.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Map<String, String[]> parasMap = getParaMap();
        if (parasMap != null){
            String start_create_time;
            String end_create_time;
            if(parasMap.get("start_create_time") != null){
                start_create_time = parasMap.get("start_create_time")[0];
                if (StringUtils.isNotBlank(start_create_time))
                    pointGiveLog.put("start_create_time",start_create_time);
            }
            if (parasMap.get("end_create_time") != null) {
                end_create_time = parasMap.get("end_create_time")[0];
                if (StringUtils.isNotBlank(end_create_time))
                    pointGiveLog.put("end_create_time",end_create_time);
            }
        }
        Page<PointGiveLog> pointGiveLogPage = PointGiveLog.dao.paginate(page, pageSize, PointGiveLog.SELECT_ALL, pointGiveLog);
        renderBuiPage(pointGiveLogPage, 200);
    }

    @Override
    public void show() {
    }

   @Override
    public void update() {
        int id = getParaToInt();
        PointGiveLog tempPointGiveLog = getModel(PointGiveLog.class);
        PointGiveLog pointGiveLog = PointGiveLog.dao.findById(id);
        pointGiveLog.cover(tempPointGiveLog);

        if (pointGiveLog.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        PointGiveLog pointGiveLog = getModel(PointGiveLog.class);
        if (pointGiveLog.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            PointGiveLog.dao.deleteById(id);
        }
        renderSuccess();
    }
}
